<script>
	var url<?=$objectid?>;
	$(document).ready(function(){
	
		newData<?=$objectid?> = function (){
			$('#window<?=$objectid?>').dialog({
				title: 'Tambah Data Calon Siswa',
				width: 600,
				height: $(window).height() * 0.98,
				closed: true,
				cache: false,
				href: base_url+'sd/psb/calon_siswa/add',
				modal: true,
				style:{background:'#dee5ff'}
			});
			 
			$('#window<?=$objectid?>').dialog('open');
			url<?=$objectid?> = base_url+'sd/psb/calon_siswa/save/add';
		}
		// end newData
		
		editData<?=$objectid?> = function (val){
			// var row = $('#dg').datagrid('getSelected');
			// if (row){
				$('#window<?=$objectid?>').dialog({
					title: 'Edit Data Calon Siswa',
					width: 600,
					height: $(window).height() * 0.98,
					closed: true,
					cache: false,
					href: base_url+'sd/psb/calon_siswa/edit/'+val,
					modal: true
				});
				
				$('#window<?=$objectid?>').dialog('open');  
				url<?=$objectid?> = base_url+'sd/psb/calon_siswa/save/edit';
			// }
		}
		// end editData
		
		deleteData<?=$objectid?> = function (val){
			// var row = $('#dg').datagrid('getSelected');
			// if(row){
				if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'sd/psb/calon_siswa/delete/' + val,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg<?=$objectid?>').datagrid('reload');
							} else {
								MessageError(response.msg);
							}
						 }
					});
				}
			// }
		}
		// end deleteData 
		
		saveData<?=$objectid?> = function(){
			$('#form1<?=$objectid?>').form('submit',{
				url: url<?=$objectid?>,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#window<?=$objectid?>').dialog('close');		// close the dialog
						$('#dg<?=$objectid?>').datagrid('reload');		// reload the user data
						if(result.id){
							print_bukti_pendaftaran<?=$objectid?>(result.id);
						}
					} else {
						ShowMessageError(result.msg);
					}
				}
			});
		}
		// end saveData
		
		daftar_ulang<?=$objectid?> = function(id){
			
			$('#window_daftar_ulang<?=$objectid?>').dialog({
				title: 'Form Daftar Ulang',
				width: 600,
				height: $(window).height() * 0.98,
				closed: true,
				cache: false,
				href: base_url+'sd/psb/daftar_ulang/add/'+id,
				modal: true
			});
			
			$('#window_daftar_ulang<?=$objectid?>').dialog('open');  
			url<?=$objectid?> = base_url+'sd/psb/daftar_ulang/save';
		}
		// end daftar ulang
		
		daftar_ulang_save<?=$objectid?> = function(){
			// get data
			//var data = $("#form1_daftar_ulang").serialize();

			//$.post(url<?=$objectid?>,($('#myForm').serialize()+'&'+$.param({ 'wordlist': wordlist })));
			//$.post(url, post_data, result);
			$.post(
				url<?=$objectid?>,
				($('#form1_daftar_ulang').serialize()),
				function(result){
					//alert(result);
					var response = eval('('+result+')');
					if(response.success){
						$.messager.show({
							title: 'Success',
							msg: 'Data Berhasil Disimpan'
						});
						$('#window_daftar_ulang<?=$objectid?>').dialog('close');
						$('#dg<?=$objectid?>').datagrid('reload');
						
						if(response.id){
							print_bukti_daftar_ulang<?=$objectid?>(response.id);
						}
					}else{
						ShowMessageError(response.msg);
					}
				}
			);
			
		}
		// end daftar ulang save
		
		// aksi pada grid cell 
		actionbutton<?=$objectid?> = function(value, row, index){
			var col="";
			
			// edit data
			if(row.status != 4){ // jika belum daftar ulang
				col += '<a href="#" onclick="editData<?=$objectid?>(\''+row.id+'\');" class="alink icon-edit" title="Edit Data"></a>';
			}

			// delete
			if(row.status == 1){ // jika masih registrasi
				col += '<a href="#" onclick="print_bukti_pendaftaran<?=$objectid?>(\''+row.id+'\');" class="alink icon-print" title="Print Bukti Daftar Ulang"></a>';
				col += '<a href="#" onclick="deleteData<?=$objectid?>(\''+row.id+'\');" class="alink icon-remove" title="Hapus Data"></a>';
			}
			
			// daftar ulang
			if(row.status == 2){ // jika sudah lulus seleksi
				col += '<a href="#" onclick="daftar_ulang<?=$objectid?>(\''+row.id+'\');" class="alink icon-daftar" title="Daftar Ulang"></a>';
			}
			
			if(row.status == 4){
				col += '<a href="#" onclick="print_bukti_daftar_ulang<?=$objectid?>(\''+row.id+'\');" class="alink icon-print" title="Print Bukti Daftar Ulang"></a>';
			}
			
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg<?=$objectid?>').datagrid({
				url:"<?=base_url()?>sd/psb/calon_siswa/grid",
				queryParams: {
					'id_psb' : $('#s_id_psb<?=$objectid?>').val(),
					'id_kelompok' : $('#s_id_kelompok<?=$objectid?>').val(),
				}
			});
		});
		
		// filter
		s_filter<?=$objectid?> = function(){
			//alert($('#s_chk_1').is(":checked"));
			$('#dg<?=$objectid?>').datagrid('load',{
				'id_psb' : $('#s_id_psb<?=$objectid?>').val(),
				'id_kelompok' : $('#s_id_kelompok<?=$objectid?>').val(),
				'chk_1' : $('#s_chk_1<?=$objectid?>').is(':checked'),
				'chk_2' : $('#s_chk_2<?=$objectid?>').is(':checked'),
				'chk_3' : $('#s_chk_3<?=$objectid?>').is(':checked'),
				'chk_4' : $('#s_chk_4<?=$objectid?>').is(':checked'),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		// auto comlite
		//$('#s_id_psb<?=$objectid?>').change(function(){
		//	$('#s_id_kelompok<?=$objectid?>').load(base_url+'prosedur/get_gelombang_by_psb/'+$('#s_id_psb<?=$objectid?>').val());
		//});
		
		//# Tombol Bawah
		/*
		$(function(){
			var pager = $('#dg<?=$objectid?>').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData<?=$objectid?>();
						}
					}
					
				]
			});			
		});
		*/
		
		// print
		print_bukti_pendaftaran<?=$objectid?> = function(id){ // id calon siswa
			popupwindow('<?=base_url()?>sd/psb/calon_siswa/print_bukti/'+id, 'Print Bukti Pendaftaran', 800, 500);
		}
		
		print_bukti_daftar_ulang<?=$objectid?> = function(id){ // id calon siswa
			popupwindow('<?=base_url()?>sd/psb/daftar_ulang/print_bukti/'+id, 'Print Bukti Daftar Ulang', 800, 500);
		}
		
	});
</script>
<table id="dg<?=$objectid?>" title="Kelola Data Calon Siswa : <?=$this->mdl_proses_penerimaan->get_title_penerimaan()?>, <?=$this->mdl_kelompok_penerimaan->get_title_kelompok()?> Tahun <?=$this->mdl_proses_penerimaan->get_title_tahun()?>" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:50,
			pageList:[50, 100, 200, 300, 500],
			toolbar:'#toolbar<?=$objectid?>',
			fit:true,
			rowStyler: function(index,row){
				if (row.status == 1){ // register
					return 'background-color:#FFF;color:#000;';
					
				}else if (row.status == 2){ // lulus
					return 'background-color:#eaffe6;color:#000;';
					
				}else if(row.status == 3){ // tidak lulus
					return 'background-color:#fff6e6;color:#000;';
					
				}else if(row.status == 4){ // terdaftar/sudah daftar ulang
					return 'background-color:#FFFFDC;color:#000;';
				}
			},
			onLoadSuccess: function(data){
				ShowGridMessage(this, 'Tidak Terdapat Data');
			},
			">
	<thead data-options="frozen:true">
		<tr>
			<th field="action" align="center" formatter="actionbutton<?=$objectid?>" width="100">Aksi</th>
		</tr>
	</thead>
	<thead>
		<tr>
			<th field="id" width="100" hidden="true"></th>
			<th field="no_pendaftaran" width="150">No. Pendaftaran</th>
			<th field="nama" width="150">Nama Calon</th>
			<th field="jk" width="70">JK</th>
			<th field="tmp_lahir" width="150">Tmp Lahir</th>
			<th field="tgl_lahir" width="100">Tgl Lahir</th>
			<th field="Agama" width="100">Agama</th>
			<th field="tlp" width="100">Telp</th>
			<th field="alamat" width="200">Alamat</th>
		</tr>
	</thead>
</table>

<div id="toolbar<?=$objectid?>" style="padding:0px;height:auto">
	<div class="fsearch">
		<div>
			<table width="100%" border="0" cellpadding="3" cellspacing="3">
			  <tr height="30px">
				<td width="50px">Status</td>
				<td>
					<label style="font-weight:normal;border:solid 1px #C2C2C2;background-color:#FFFFFF;margin:auto 5px;padding:1px 2px;width:auto;"><input id="s_chk_1<?=$objectid?>" type="checkbox" checked="checked" /> Daftar&nbsp;&nbsp;</label>
					<label style="font-weight:normal;border:solid 1px #C2C2C2;background-color:#eaffe6;margin:auto 5px;padding:1px 2px;width:auto;"><input id="s_chk_2<?=$objectid?>" type="checkbox" checked="checked" /> Lulus&nbsp;&nbsp;</label>
					<label style="font-weight:normal;border:solid 1px #C2C2C2;background-color:#fff6e6;margin:auto 5px;padding:1px 2px;width:auto;"><input id="s_chk_3<?=$objectid?>" type="checkbox" checked="checked" /> Tidak Lulus&nbsp;&nbsp;</label>
					<label style="font-weight:normal;border:solid 1px #C2C2C2;background-color:#FFFFDC;margin:auto 5px;padding:1px 2px;width:auto;"><input id="s_chk_4<?=$objectid?>" type="checkbox" checked="checked" /> Sudah Daftar Ulang&nbsp;&nbsp;</label>
				</td>
			  </tr>
			  <tr style="display:none;">
				<td width="150px">Proses penerimaan</td>
				<td>: 
					<select name="s_id_psb" id="s_id_psb<?=$objectid?>" style="width:230px;">
						<?=$this->mdl_prosedur->option_proses_penerimaan();?>
					</select>
				</td>
			  </tr>
			  <tr style="display:none;">
				<td>Gelombang</td>
				<td>: 
					<select name="s_id_kelompok" id="s_id_kelompok<?=$objectid?>" style="width:230px;">
						<?=$this->mdl_prosedur->option_kelompok_penerimaan()?>
					</select>
				</td>
			  </tr>
			  
			  <tr>
				<td>&nbsp;</td>				
				<td>
					<a href="#" onclick="s_filter<?=$objectid?>()" class="easyui-linkbutton" iconCls="icon-search" style="width:100px;margin:1px 3px;">Filter</a>
					<a href="#" onclick="newData<?=$objectid?>()" class="easyui-linkbutton" iconCls="icon-add" style="width:130px;margin:1px 3px;">Tambah Baru</a>
				</td>
			  </tr>
			</table>
		</div>
	</div>
</div>


<!-- AREA untuk Form Add/EDIT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  -->
<div id="window<?=$objectid?>" class="easyui-dialog" style="width:400px;height:150px;padding:10px;" data-options="
		closed:true,
		minimizable:false,
		collapsible:false,
		maximizable:false,
		buttons:[{
					text:'Save',
					iconCls:'icon-ok',
					handler:function(){ saveData<?=$objectid?>(); }
				},{
					text:'Close',
					iconCls:'icon-cancel',
					handler:function(){ $('#window<?=$objectid?>').dialog('close'); }
				}],
		onResize:function(){
			$(this).dialog('center');
		}"
>
</div>


<div id="window_daftar_ulang<?=$objectid?>" class="easyui-dialog" style="width:400px;height:150px;padding:10px;" data-options="
		closed:true,
		minimizable:false,
		collapsible:false,
		maximizable:false,
		buttons:[{
					text:'Simpan',
					iconCls:'icon-ok',
					handler:function(){ daftar_ulang_save<?=$objectid?>(); }
				},{
					text:'Batal',
					iconCls:'icon-cancel',
					handler:function(){ $('#window_daftar_ulang<?=$objectid?>').dialog('close'); }
				}],
		onResize:function(){
			$(this).dialog('center');
		}"
>
</div>